package com.mall.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.dubbo.mappers.*;
import com.tanhua.model.domain.*;
import com.tanhua.model.dto.ProductListDto;
import com.tanhua.model.vo.PmsProductParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Slf4j
@DubboService
public class PmsProductApiImpl implements PmsProductApi{



    @Autowired
    private PmsProductMapper pmsProductMapper;
    @Autowired
    private PmsMemberPriceMapper pmsMemberPriceMapper; //商品会员价格设置
    @Autowired
    private CmsPrefrenceAreaProductRelationMapper cmsPrefrenceAreaProductRelationMapper;//优选专区和商品的关系
    @Autowired
    private PmsProductAttributeValueMapper pmsProductAttributeValueMapper;// 商品参数及自定义规格属性
    @Autowired
    private PmsProductFullReductionMapper pmsProductFullReductionMapper; //商品满减价格设置
    @Autowired
    private PmsProductLadderMapper pmsProductLadderMapper;  //商品阶梯价格设置
    @Autowired
    private PmsSkuStockMapper pmsSkuStockMapper;  //商品的sku库存信息
    @Autowired
    private CmsSubjectProductRelationMapper cmsSubjectProductRelationMapper; //专题和商品关系

    @Autowired
    private PmsProductCategoryMapper pmsProductCategory;



    /**
     * 查询商品列表
     * @param productListDto
     * @return
     */
    @Override
    public Page getList(ProductListDto productListDto) {



        Page<PmsProduct> pmsProductPage = new Page<>(productListDto.getPageNum(),productListDto.getPageSize());
        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();


        lqw.eq(productListDto.getBrandId() != null, PmsProduct::getBrandId, productListDto.getBrandId()).
                like(productListDto.getKeyword() != null, PmsProduct::getKeywords, productListDto.getKeyword()).
                eq(productListDto.getProductCategoryId() != null, PmsProduct::getProductCategoryId, productListDto.getProductCategoryId()).
                eq(productListDto.getProductSn() != null, PmsProduct::getProductSn, productListDto.getProductSn()).
                eq(productListDto.getPublishStatus() != null, PmsProduct::getPublishStatus, productListDto.getPublishStatus()).
                eq(productListDto.getVerifyStatus() != null, PmsProduct::getVerifyStatus, productListDto.getVerifyStatus())
                .eq(PmsProduct::getDeleteStatus,0);


        return pmsProductMapper.selectPage(pmsProductPage, lqw);
    }

    /**
     * 商品列表批量上下架商品
     * @param ids
     * @param publishStatus
     * @return
     */
    @Override
    public void updatePublishStatus(List<Long> ids, Integer publishStatus) {
        for (Long id : ids) {
            PmsProduct pmsProduct = new PmsProduct();
            pmsProduct.setId(id);
            pmsProduct.setPublishStatus(publishStatus);
            pmsProductMapper.updateById(pmsProduct);
        }

    }

    /**
     * 批量上下架商品
     * @param ids
     * @param publishStatus
     * @return
     */
    @Override
    public void updateNewStatus(List<Long> ids, Integer publishStatus) {
        for (Long id : ids) {
            PmsProduct pmsProduct = new PmsProduct();
            pmsProduct.setId(id);
            pmsProduct.setPublishStatus(publishStatus);
            pmsProductMapper.updateById(pmsProduct);
        }

    }

    /**
     * 批量推荐与否
     * @param ids
     * @param publishStatus
     * @return
     */
    @Override
    public void updateRecommendStatus(List<Long> ids, Integer publishStatus) {
        for (Long id : ids) {
            PmsProduct pmsProduct = new PmsProduct();
            pmsProduct.setId(id);
            pmsProduct.setPublishStatus(publishStatus);
            pmsProductMapper.updateById(pmsProduct);
        }

    }




    /**
     *  @Autowired
     *     private PmsMemberPriceMapper pmsMemberPriceMapper; //商品会员价格设置
     *     @Autowired
     *     private CmsPrefrenceAreaProductRelationMapper cmsPrefrenceAreaProductRelationMapper;//优选专区和商品的关系
     *     @Autowired
     *     private PmsProductAttributeValueMapper pmsProductAttributeValueMapper;// 商品参数及自定义规格属性
     *     @Autowired
     *     private PmsProductFullReductionMapper pmsProductFullReductionMapper; //商品满减价格设置
     *     @Autowired
     *     private PmsProductLadderMapper pmsProductLadderMapper;  //商品阶梯价格设置
     *     @Autowired
     *     private PmsSkuStockMapper pmsSkuStockMapper;  //商品的sku库存信息
     *     @Autowired
     *     private CmsSubjectProductRelationMapper cmsSubjectProductRelationMapper; //专题和商品关系
     *
     */
    /**
     * 添加商品
     * @param pmsProductParam
     */
    @Override
    public void productCreate(PmsProductParam pmsProductParam) {
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtils.copyProperties(pmsProductParam,pmsProduct);
        pmsProductMapper.insert(pmsProduct);

        this.add(pmsProductParam, pmsProduct);
    }

    //添加商品
    private void add(PmsProductParam pmsProductParam, PmsProduct pmsProduct) {
        //商品会员价格设置
        List<PmsMemberPrice> memberPriceList = pmsProductParam.getMemberPriceList();
        for (PmsMemberPrice pmsMemberPrice : memberPriceList) {
            pmsMemberPrice.setProductId(pmsProduct.getId());
            pmsMemberPriceMapper.insert(pmsMemberPrice);
        }

        //优选专区和商品的关系
        List<CmsPrefrenceAreaProductRelation> prefrenceAreaProductRelationList = pmsProductParam.getPrefrenceAreaProductRelationList();
        for (CmsPrefrenceAreaProductRelation cmsPrefrenceAreaProductRelation : prefrenceAreaProductRelationList) {
            cmsPrefrenceAreaProductRelation.setProductId(pmsProduct.getId());
            cmsPrefrenceAreaProductRelationMapper.insert(cmsPrefrenceAreaProductRelation);
        }

        //商品参数及自定义规格属性
        List<PmsProductAttributeValue> productAttributeValueList = pmsProductParam.getProductAttributeValueList();
        for (PmsProductAttributeValue pmsProductAttributeValue : productAttributeValueList) {
            pmsProductAttributeValue.setProductId(pmsProduct.getId());
            pmsProductAttributeValueMapper.insert(pmsProductAttributeValue);
        }

        //商品满减价格设置
        List<PmsProductFullReduction> productFullReductionList = pmsProductParam.getProductFullReductionList();
        for (PmsProductFullReduction pmsProductFullReduction : productFullReductionList) {
            pmsProductFullReduction.setProductId(pmsProduct.getId());
            pmsProductFullReductionMapper.insert(pmsProductFullReduction);
        }

        //商品阶梯价格设置
        List<PmsProductLadder> productLadderList = pmsProductParam.getProductLadderList();
        for (PmsProductLadder pmsProductLadder : productLadderList) {
            pmsProductLadder.setProductId(pmsProduct.getId());
            pmsProductLadderMapper.insert(pmsProductLadder);
        }

        //商品的sku库存信息
        List<PmsSkuStock> skuStockList = pmsProductParam.getSkuStockList();
        for (PmsSkuStock pmsSkuStock : skuStockList) {
            pmsSkuStock.setProductId(pmsProduct.getId());
            pmsSkuStockMapper.insert(pmsSkuStock);
        }

        //专题和商品关系
        List<CmsSubjectProductRelation> subjectProductRelationList = pmsProductParam.getSubjectProductRelationList();
        for (CmsSubjectProductRelation cmsSubjectProductRelation : subjectProductRelationList) {
            cmsSubjectProductRelation.setProductId(pmsProduct.getId());
            cmsSubjectProductRelationMapper.insert(cmsSubjectProductRelation);
        }
    }

    /**
     * 修改商品时回显
     * @param id
     * @return
     */
    @Override
    public PmsProductParam updateInfoById(Long id) {
        PmsProductParam pmsProductParam = new PmsProductParam();

        PmsProduct pmsProduct = pmsProductMapper.selectById(id);
        BeanUtils.copyProperties(pmsProduct,pmsProductParam);


        //商品会员价格查询
        LambdaQueryWrapper<PmsMemberPrice> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(PmsMemberPrice::getProductId,id);
        List<PmsMemberPrice> pmsMemberPrices = pmsMemberPriceMapper.selectList(lqw1);
        pmsProductParam.setMemberPriceList(pmsMemberPriceMapper.selectList(lqw1));

        //优选专区和商品的关系
        LambdaQueryWrapper<CmsPrefrenceAreaProductRelation> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(CmsPrefrenceAreaProductRelation::getProductId,id);
        List<CmsPrefrenceAreaProductRelation> cmsPrefrenceAreaProductRelations = cmsPrefrenceAreaProductRelationMapper.selectList(lqw2);
        pmsProductParam.setPrefrenceAreaProductRelationList(cmsPrefrenceAreaProductRelationMapper.selectList(lqw2));

        //商品参数及自定义规格属性
        LambdaQueryWrapper<PmsProductAttributeValue> lqw3 = new LambdaQueryWrapper<>();
        lqw3.eq(PmsProductAttributeValue::getProductId,id);
        List<PmsProductAttributeValue> pmsProductAttributeValues = pmsProductAttributeValueMapper.selectList(lqw3);
        pmsProductParam.setProductAttributeValueList(pmsProductAttributeValueMapper.selectList(lqw3));

        //商品满减价格设置
        LambdaQueryWrapper<PmsProductFullReduction> lqw4 = new LambdaQueryWrapper<>();
        lqw4.eq(PmsProductFullReduction::getProductId,id);
        List<PmsProductFullReduction> pmsProductFullReductions = pmsProductFullReductionMapper.selectList(lqw4);
        pmsProductParam.setProductFullReductionList(pmsProductFullReductionMapper.selectList(lqw4));

        //商品阶梯价格设置
        LambdaQueryWrapper<PmsProductLadder> lqw5 = new LambdaQueryWrapper<>();
        lqw5.eq(PmsProductLadder::getProductId,id);
        List<PmsProductLadder> pmsProductLadders = pmsProductLadderMapper.selectList(lqw5);
        pmsProductParam.setProductLadderList(pmsProductLadderMapper.selectList(lqw5));

        //商品的sku库存信息
        LambdaQueryWrapper<PmsSkuStock> lqw6 = new LambdaQueryWrapper<>();
        lqw6.eq(PmsSkuStock::getProductId,id);
        List<PmsSkuStock> pmsSkuStocks = pmsSkuStockMapper.selectList(lqw6);
        pmsProductParam.setSkuStockList(pmsSkuStockMapper.selectList(lqw6));

        //专题和商品关系
        LambdaQueryWrapper<CmsSubjectProductRelation> lqw7 = new LambdaQueryWrapper<>();
        lqw7.eq(CmsSubjectProductRelation::getProductId,id);
        List<CmsSubjectProductRelation> cmsSubjectProductRelations = cmsSubjectProductRelationMapper.selectList(lqw7);
        pmsProductParam.setSubjectProductRelationList(cmsSubjectProductRelationMapper.selectList(lqw7));




        pmsProductParam.setCateParentId(pmsProductCategory.selectById(pmsProduct.getProductCategoryId()).getParentId());

        return pmsProductParam;
    }

    /**
     * 逻辑删除
     * @param ids
     * @param deleteStatus
     */
    @Override
    public void updateDeleteStatus(List<Long> ids, Integer deleteStatus) {
        for (Long id : ids) {
            PmsProduct pmsProduct = new PmsProduct();
            pmsProduct.setId(id);
            pmsProduct.setDeleteStatus(deleteStatus);
            pmsProductMapper.updateById(pmsProduct);
        }
    }

    /**
     * 更新商品
     * @param id
     */
    @Override
    public void updateById(Long id,PmsProductParam pmsProductParam) {
        //先对表中数据修改
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtils.copyProperties(pmsProductParam,pmsProduct);
        pmsProductMapper.updateById(pmsProduct);

        //先将其他表数据删除

        //商品会员价格查询
        LambdaQueryWrapper<PmsMemberPrice> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(PmsMemberPrice::getProductId,id);
        pmsMemberPriceMapper.delete(lqw1);

        //优选专区和商品的关系
        LambdaQueryWrapper<CmsPrefrenceAreaProductRelation> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(CmsPrefrenceAreaProductRelation::getProductId,id);
        cmsPrefrenceAreaProductRelationMapper.delete(lqw2);

        //商品参数及自定义规格属性
        LambdaQueryWrapper<PmsProductAttributeValue> lqw3 = new LambdaQueryWrapper<>();
        lqw3.eq(PmsProductAttributeValue::getProductId,id);
        pmsProductAttributeValueMapper.delete(lqw3);

        //商品满减价格设置
        LambdaQueryWrapper<PmsProductFullReduction> lqw4 = new LambdaQueryWrapper<>();
        lqw4.eq(PmsProductFullReduction::getProductId,id);
        pmsProductFullReductionMapper.delete(lqw4);

        //商品阶梯价格设置
        LambdaQueryWrapper<PmsProductLadder> lqw5 = new LambdaQueryWrapper<>();
        lqw5.eq(PmsProductLadder::getProductId,id);
        pmsProductLadderMapper.delete(lqw5);

        //商品的sku库存信息
        LambdaQueryWrapper<PmsSkuStock> lqw6 = new LambdaQueryWrapper<>();
        lqw6.eq(PmsSkuStock::getProductId,id);
        pmsSkuStockMapper.delete(lqw6);

        //专题和商品关系
        LambdaQueryWrapper<CmsSubjectProductRelation> lqw7 = new LambdaQueryWrapper<>();
        lqw7.eq(CmsSubjectProductRelation::getProductId,id);
        cmsSubjectProductRelationMapper.delete(lqw7);

        this.add(pmsProductParam,pmsProduct);
    }

    @Override
    public List<PmsProduct> productSimpleList(String keyword) {
        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.like(keyword != null,PmsProduct::getName,keyword).
                or().like(keyword != null,PmsProduct::getProductSn,keyword);
        return pmsProductMapper.selectList(lqw);
    }
}
